<?php

namespace App\Service;

use App\Model\ConfigBannerModel;
use App\Model\ConfigDailiModel;
use App\Model\ConfigFreeServiceModel;
use App\Model\ConfigHouseModel;
use App\Model\ConfigNavModel;
use App\Model\ConfigNewsTypeModel;
use App\Model\ConfigOneKeyModel;
use App\Model\ConfigPatrolItemModel;
use App\Model\ConfigPatrolModel;
use App\Model\ConfigRentModel;
use App\Model\ConfigRepairItemModel;
use App\Model\ConfigRepairModel;
use App\Model\ConfigShareItemModel;
use App\Model\ConfigShareModel;
use App\Model\ConfigSplashModel;
use App\Model\ConfigTitleModel;
use App\Model\ShareToolNavModel;
use App\Model\ShopGoodsCateModel;
use App\Utility\DataConversion;
use EasySwoole\ORM\Collection\Collection;
use EasySwoole\ORM\Db\Cursor;
use EasySwoole\ORM\Db\CursorInterface;
use EasySwoole\ORM\Exception\Exception;
use JetBrains\PhpStorm\ArrayShape;
use Throwable;

class ConfigureService extends BaseService
{
    /**
     * @param $nid
     * @return array
     * @throws Exception
     * @throws Throwable
     */
    public function getHouse($nid): array
    {
        $houses = ConfigHouseModel::create()->where(['is_deleted' => 0])->all(['nid' => $nid]);

        if (empty($houses)) {
            return [
                'result' => false,
                'msg' => '暂无数据'
            ];
        }

        $data = [];
        foreach ($houses as $house) {
            $data[] = [
                'id' => $house['id'],
                'name' => $house['name'],
            ];
        }

        return [
            'result' => true,
            'msg' => '获取成功',
            'data' => $data
        ];
    }

    /**
     * @param $type
     * @return array
     * @throws Exception
     * @throws Throwable
     */
    public function getRepairType($type): array
    {
        $repairs = ConfigRepairModel::create()->all(['is_deleted' => 0, 'type' => $type]);

        if (empty($repairs)) {
            return [
                'result' => false,
                'msg' => '暂无数据'
            ];
        }

        $data = [];
        foreach ($repairs as $repair) {
            $data[] = [
                'id' => $repair['id'],
                'icon' => $repair['icon'],
                'name' => $repair['name'],
                'title' => $repair['name']
            ];
        }

        return [
            'result' => true,
            'msg' => '获取成功',
            'data' => $data
        ];
    }

    /**
     * @param $rid
     * @return array
     * @throws Exception
     * @throws Throwable
     * @throws \EasySwoole\Mysqli\Exception\Exception
     */
    public function getRepairDetail($rid): array
    {
        $repair = ConfigRepairModel::create()->getConfig(['id' => $rid]);
        if (!$repair) {
            return [
                'result' => false,
                'msg' => '该类型已下架'
            ];
        }

        $detail = $repair->toArray();
        $project = ConfigRepairItemModel::create()->all(['is_deleted' => 0, 'nid' => $rid]);
        $data = [];
        foreach ($project as $item) {
            $data[] = [
                'name' => $item['name'],
                'price' => $item['price'],
                'unit' => $item['unit'],
                'memo' => $item['memo'],
            ];
        }

        $detail['project'] = $data;
        return [
            'result' => true,
            'msg' => '获取成功',
            'data' => $detail
        ];
    }

    /**
     * @return array
     * @throws Exception
     * @throws Throwable
     */
    public function getHomeNav(): array
    {
        $navs = ConfigNavModel::create()->where(['is_deleted' => 0])->order('sort', 'DESC')->all();
        $data = [];
        foreach ($navs as $nav) {
            $data[] = [
                'id' => $nav['id'],
                'method' => $nav['method'],
                'name' => $nav['name'],
                'icon' => $nav['icon']
            ];
        }

        return [
            'result' => true,
            'msg' => '获取成功',
            'data' => $data
        ];
    }

    /**
     * @return array
     * @throws Exception
     * @throws Throwable
     */
    public function getAdjacentNav(): array
    {
        $navs = ConfigShareModel::create()->where(['is_deleted' => 0])->all();
        $data = [];
        foreach ($navs as $nav) {
            $data[] = [
                'id' => $nav['id'],
                'name' => $nav['name'],
                'icon' => $nav['icon'],
                'image' => $nav['image']
            ];
        }

        return [
            'result' => true,
            'msg' => '获取成功',
            'data' => $data
        ];
    }

    /**
     * @param $type
     * @return array
     * @throws Exception
     * @throws Throwable
     */
    public function getBanner($type): array
    {
        $banners = ConfigBannerModel::create()->where(['type' => $type, 'is_deleted' => 0])->all();
        return [
            'result' => true,
            'msg' => '获取成功',
            'data' => $banners
        ];
    }

    /**
     * @return array
     * @throws Exception
     * @throws Throwable
     */
    public function getOneKey(): array
    {
        $data = ConfigOneKeyModel::create()->where(['is_deleted' => 0])->order('sort', 'DESC')->all();
        return [
            'result' => true,
            'msg' => '获取成功',
            'data' => $data
        ];
    }

    /**
     * @param $id
     * @return array
     * @throws Exception
     * @throws Throwable
     */
    public function getShareService($id): array
    {
        $service = ConfigShareItemModel::create()->where(['nid' => $id])->all();
        if (empty($service)) {
            return [
                'result' => false,
                'msg' => '暂无更多服务'
            ];
        }

        return [
            'result' => true,
            'msg' => '获取成功',
            'data' => $service
        ];
    }

    /**
     * @param $id
     * @return array
     * @throws Exception
     * @throws Throwable
     * @throws \EasySwoole\Mysqli\Exception\Exception
     */
    public function getArticle($id): array
    {
        $banner = ConfigBannerModel::create()->getBanner(['id' => $id]);
        if (!$banner) {
            return [
                'result' => false,
                'msg' => '暂无更多服务'
            ];
        }

        $banner = $banner->toArray();
        $data = [
            'id' => $banner['id'],
            'title' => $banner['title'],
            'image' => $banner['image'],
            'content' => $banner['content'],
            'author' => $banner['author'],
            'time' => DataConversion::getInstance()->timeToDate($banner['create_time'])
        ];

        return [
            'result' => true,
            'msg' => '获取成功',
            'data' => $data
        ];
    }

    /**
     * @return array
     * @throws Exception
     * @throws Throwable
     */
    public function getFreeService(): array
    {
        $data = ConfigFreeServiceModel::create()->where(['is_deleted' => 0])->order('sort', 'DESC')->all();
        return [
            'result' => true,
            'msg' => '获取成功',
            'data' => $data
        ];
    }

    /**
     * @return array
     * @throws Exception
     * @throws Throwable
     */
    public function getPatrol(): array
    {
        $list = ConfigPatrolModel::create()->where(['is_deleted' => 0])->all();
        $data = [];
        foreach ($list as $item) {
            $checkList = explode(',', $item->item);
            $value = [];
            if (!empty($checkList)) {
                $value = ConfigPatrolItemModel::create()->where("id", $checkList, "IN")->all();
            }
            $checkData = [];
            foreach ($value as $i) {
                $checkData[] = [
                    'name' => $i['name'],
                    'type' => $i['type'],
                    'select' => explode('|', $i['description'])
                ];
            }
            $item['content'] = $checkData;
            $data[] = $item;
        }
        return [
            'result' => true,
            'msg' => '获取成功',
            'data' => $data
        ];
    }

    /**
     * @return array
     * @throws Exception
     * @throws Throwable
     */
    public function getShopNavS(): array
    {
        $cates = ShopGoodsCateModel::create()->where(['is_deleted' => 0, 'pid' => 0])->order('sort', 'desc')->all();
        $cate1 = [];
        $cate2 = [];
        $i = count($cates) / 2;
        foreach ($cates as $k => $v) {
            if ($k >= $i) {
                $cate2[] = $v;
            } else {
                $cate1[] = $v;
            }
        }

        $data = [
            $cate1, $cate2
        ];

        return [
            'result' => true,
            'msg' => '获取成功',
            'data' => $data
        ];
    }

    /**
     * @return array
     * @throws Exception
     * @throws Throwable
     */
    public function getShopNavH(): array
    {
        $cates = ShopGoodsCateModel::create()->where(['is_deleted' => 0, 'pid' => 0, 'status' => 1])->order('sort', 'desc')->limit(3)->all();
        $data = [];
        foreach ($cates as $v) {
            $data[] = [
                'id' => $v['id'],
                'name' => $v['name'],
                'cover' => $v['cover'],
                'child' => $this->shopNavHomeS($v['id'])
            ];
        }

        return [
            'result' => true,
            'msg' => '获取成功',
            'data' => $data
        ];
    }

    /**
     * @param $pid
     * @return array|bool|Collection|Cursor|CursorInterface
     * @throws Exception
     * @throws Throwable
     */
    public function shopNavHomeS($pid)
    {
        return ShopGoodsCateModel::create()->where(['is_deleted' => 0, 'pid' => $pid, 'status' => 1])->order('sort', 'desc')->limit(3)->all();
    }

    /**
     * @return array
     * @throws Exception
     * @throws Throwable
     */
    public function getNewsType(): array
    {
        $data = ConfigNewsTypeModel::create()->where(['is_deleted' => 0])->all();
        return [
            'result' => true,
            'msg' => '获取成功',
            'data' => $data
        ];
    }

    /**
     * @return array
     * @throws Exception
     * @throws Throwable
     */
    public function getTitle(): array
    {
        $data = ConfigTitleModel::create()->where(['is_deleted' => 0])->field('name,icon,method')->all();
        return [
            'result' => true,
            'msg' => '获取成功',
            'data' => array_column($data, 'icon', 'method')
        ];
    }

    /**
     * @return array
     * @throws Exception
     * @throws Throwable
     */
    public function getSplash(): array
    {
        $data = ConfigSplashModel::create()->where(['is_deleted' => 0])->order('sort', 'desc')->all();
        return [
            'result' => true,
            'msg' => '获取成功',
            'data' => $data
        ];
    }

    /**
     * @return array
     * @throws Exception
     * @throws Throwable
     */
    public function getRentType(): array
    {
        $userSelect = ConfigRentModel::create()->where(['is_deleted' => 0, 'nid' => 0])->all();
        $data = [];
        if (!$userSelect) return [
            'result' => false,
            'msg' => '暂无数据'
        ];;
        foreach ($userSelect as $k => $tag) {
            $data[$tag['name']] = $this->rent_filter($tag['id']);
        }
        return [
            'result' => true,
            'msg' => '获取成功',
            'data' => $data
        ];
    }

    /**
     * @param $nid
     * @return array
     * @throws Exception
     * @throws Throwable
     */
    public function rent_filter($nid): array
    {
        $tags = ConfigRentModel::create()->order('name', 'ASC')->all(['nid' => $nid, 'is_deleted' => 0]);
        $data = [];
        if (!$tags) return $data;
        foreach ($tags as $k => $tag) {
            if ($tag['nid'] == $nid)
                $data[$k] = [
                    'id' => $tag['id'],
                    'value' => $tag['name'],
                    'label' => $tag['label'],
                    'name' => $tag['label'],
                    'text' => $tag['label'],
                    'checked' => false,
                    'disabled' => false
                ];
        }
        return $data;
    }
}